package com.feng.system.controller;

import com.feng.common.SystemConstants;
import com.feng.core.Result;
import com.feng.system.domain.SysUser;
import com.feng.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息
 *
 * @author RoyWang
 */
@RestController
@RequestMapping("/system/user")
public class SysUserController {

    @Autowired
    private ISysUserService userService;

    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    public Result list(SysUser user) {
        List<SysUser> userList = userService.selectUserList(user);
        return Result.success(userList);
    }

    /**
     * 新增用户
     */
    @PostMapping("/add")
    public Result add(@RequestBody SysUser user) {
        if (SystemConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user))) {
            return Result.error("用户名称已存在, 新增失败");
        }
        return Result.request(userService.insertUser(user));
    }

    /**
     * 根据用户ID获取信息
     */
    @GetMapping(value = "/getUserById/{userId}")
    public Result getInfo(@PathVariable Integer userId) {
        return Result.success(userService.selectUserById(userId));
    }

    /**
     * 修改用户
     */
    @PostMapping("/update")
    public Result updateUser(@RequestBody SysUser user) {
        return Result.request(userService.updateUser(user));
    }

    /**
     * 删除用户
     */
    @DeleteMapping("{userId}")
    public Result remove(@PathVariable Integer userId) {
        return Result.request(userService.deleteUserById(userId));
    }

}
